package com.gz.logistics.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gz.logistics.R;
import com.gz.logistics.adapter.QueryOrderAdapter;
import com.gz.logistics.adapter.QueryOrderAdapterY;
import com.gz.logistics.adapter.SpinnerAdapter;
import com.gz.logistics.app.Define;
import com.gz.logistics.app.OrderDetailActivity;
import com.gz.logistics.library.IAuthor;
import com.gz.logistics.library.IOrder;
import com.gz.logistics.library.listener.GetCommunityBuildingListener;
import com.gz.logistics.library.listener.GetCommunityGroupListener;
import com.gz.logistics.library.listener.GetQueryorderListener;
import com.gz.logistics.library.listener.GetQueryorderListenerY;
import com.gz.logistics.library.listener.GetStoreHouseListener;
import com.gz.logistics.library.listener.RequestCode;
import com.gz.logistics.library.pojo.Building;
import com.gz.logistics.library.pojo.CommunityGroup;
import com.gz.logistics.library.pojo.CustomerSpinnerObject;
import com.gz.logistics.library.pojo.OrderInfo;
import com.gz.logistics.library.pojo.OrderInfoY;
import com.gz.logistics.library.pojo.StoreHouse;
import com.gz.logistics.library.pojo.UserInfo;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.CrashHandler;
import com.gz.logistics.library.util.PreferenceModel;
import com.gz.logistics.util.ToastUtils;
import com.gz.logistics.widget.DoubleDatePickerDialog;
import com.gz.logistics.widget.FooterView;
import com.gz.logistics.widget.LoadingCover;
import com.gz.logistics.widget.SpinerPopWindow;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class QureyOrderFragment extends Fragment implements View.OnClickListener,
        PullToRefreshBase.OnRefreshListener,
        LoadingCover.OnLoadingCoverRefreshListener, FooterView.OnFooterViewRefreshListener,DoubleDatePickerDialog.OnDateSetListener {
    private String TAG = QureyOrderFragment.class.getSimpleName();
    private List<OrderInfo> mDataList;
    private List<OrderInfoY> mDataLists;
    private QueryOrderAdapter mAdapter;//查询未签收订单的适配器
    private QueryOrderAdapterY mAdapterY;//查询已签收订单的适配器
    private PullToRefreshListView mPullListW;//未签收list
    private PullToRefreshListView mPullListY;//已签收list
    private LoadingCover mLoadingCover;
    private View mConvertView;
    private Context mContext;
    private UserInfo userInfo;
    private FooterView mFooterView;
    private Dialog mLoadingData;

    private myGetStoreListener myGetStoreListener;//查询仓库的监听器
    private myGetGroupListener myGetGroupListener;//查询小区的监听器
    private myGetBuildListener myGetBuildListener;//查询单元楼监听器
    private MyGetQueryorderListener myGetOrderListener;

    private SpinnerAdapter partSpinnerAdapter;
    private SpinnerAdapter groupSpinnerAdapter;
    private SpinnerAdapter buildSpinnerAdapter;

    private MyPartSpinnerOnItemSelectListener mPartSpinnerListener;
    private MyGroupSpinnerOnItemSelectListener mGroupSpinnerListener;
    private MyBuildSpinnerOnItemSelectListener mBuildSpinnerListener;

    private SpinerPopWindow mPartSpinnerPopWindow; //仓库下拉窗口
    private SpinerPopWindow mGroupSpinnerPopWindow;//小区的下拉窗口
    private SpinerPopWindow mBuildSpinnerPopWindow;//单元楼下拉窗口

    private List<CustomerSpinnerObject> storehouse;// 仓库列表
    private List<CustomerSpinnerObject> grouplist;// 小区列表
    private List<CustomerSpinnerObject> buildinglist;// 楼房列表
    private MyHandler mHandler;

    private TextView iv_actionbar_title;
    private TextView tv_actionbar_deliveryName;
    private RelativeLayout rl_choice_for_part;   //顶部仓库
    private RelativeLayout rl_choice_for_status; //时间
    private RelativeLayout rl_choice_for_order;//顶部小区
    private RelativeLayout rl_choice_for_building;//顶部单元楼

    private TextView tv_part_name;
    private TextView tv_status;
    private TextView tv_order;
    private TextView tv_building;
    private TextView status_2;//未签收
    private TextView status_1;//已签收

    private int page = 1;
    private RefreshReceiver mRefreshReceiver;
    private int storehouseid = 0;//仓库id
    private int groupid = 0;//小区id
    private int buildid = 0;//单元楼id
    private String status = "1";//订单的签收状态
    private DoubleDatePickerDialog timeDialog;
    private Calendar mCalendar;
    private String startTime = null; //开始时间
    private String endTime = null;//结束时间
    private TextView num;//未签收的数量
    private Date date;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRefreshReceiver = new RefreshReceiver();
        IntentFilter intentFilter = new IntentFilter(Define.ACTION_REFRESH_SHOPPING_CART);
        getActivity().registerReceiver(mRefreshReceiver, intentFilter);
        // PushAgent.getInstance(mContext).onAppStart();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mConvertView == null){
            mConvertView = inflater.inflate(R.layout.fragment_qurey_order,container,false);
            init(inflater);
        }
        initAdapter();
        mHandler = new MyHandler(this);
        initListener();

        return mConvertView;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.release();
        myGetStoreListener.release();
        myGetGroupListener.release();
        myGetBuildListener.release();
        mPartSpinnerListener.release();
        mGroupSpinnerListener.release();
        mBuildSpinnerListener.release();
        myGetOrderListener.release();
        // ((ViewGroup)mConvertView.getParent()).removeView(mConvertView);
    }
    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * 接收到的广播
     */
    private class RefreshReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (status.equals("1")){
                loadDataFromServer(buildListener(RequestCode.PULL));
            }else if(status.equals("2")){
                loadbuildingData();
            }

        }
    }

    /**
     * 片区选择 和 状态选择 button 点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_choice_for_part:
                if(storehouse.size() <= 0 ){
                    ToastUtils.showToast(getActivity(),"仓库列表为空");
                    return ;
                }
                mPartSpinnerPopWindow.setWidth(rl_choice_for_part.getWidth()*2);
                mPartSpinnerPopWindow.showAsDropDown(rl_choice_for_part);
                break;
            case R.id.rl_choice_for_status://时间
                timeDialog.show();
                break;
            case R.id.rl_choice_for_building://单元楼
                if (buildinglist.size() <= 0){
                    ToastUtils.showToast(getActivity(),"单元楼列表为空");
                    return;
                }
                mBuildSpinnerPopWindow.setWidth(rl_choice_for_building.getWidth()*2);
                mBuildSpinnerPopWindow.setHeight(350);
                mBuildSpinnerPopWindow.showAsDropDown(rl_choice_for_building);
                break;
            case R.id.rl_choice_for_order://小区
                if(grouplist.size() <= 0){
                    ToastUtils.showToast(getActivity(),"小区列表为空");
                    return;
                }
                mGroupSpinnerPopWindow.setWidth(rl_choice_for_order.getWidth()*2);
                mGroupSpinnerPopWindow.setHeight(350);
                mGroupSpinnerPopWindow.showAsDropDown(rl_choice_for_order);
                break;
            case R.id.status_1://已签收按钮
                mLoadingData.show();
                status = "1";
                storehouseid = 0;
                groupid = 0;
                buildid = 0;
                date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                if (startTime==null&&endTime==null){
                    endTime = format.format(new Date(date.getTime()))+" 23:59:59";//系统当前的时间
                    startTime = format.format(new Date(date.getTime()))+" 00:00:00";//当天凌晨
                }
                status_1.setBackgroundResource(R.drawable.bg_my_orders_frame_orange);
                status_2.setBackgroundResource(R.drawable.bg_my_orders_frame_gray);
                loadDataFromServer(buildListener(RequestCode.PULL));
                break;
            case R.id.status_2:
                mLoadingData.show();
                status = "2";
                storehouseid = 0;
                groupid = 0;
                buildid = 0;
                startTime = null;
                endTime = null;
                status_2.setBackgroundResource(R.drawable.bg_my_orders_frame_orange);
                status_1.setBackgroundResource(R.drawable.bg_my_orders_frame_gray);
                loadbuildingData();
                break;

        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Define.ACTIVITY_CODE_ORDER_DETAIL && resultCode == Define.ACTIVITY_CODE_ORDER_DETAI_CHANGE){
            if (data != null){

            }
        }

    }
    @Override
    public void onLoadingCoverRefresh() {
        if (status.equals("1")){
            loadDataFromServer(buildListener(RequestCode.PULL));
        }else{
            loadbuildingData();
        }
    };

    /**
     * 下拉刷新
     * @param refreshView
     */
    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        loadDataFromServer(buildListener(RequestCode.PULL));

    }
    /**
     * 加载更多
     */
    @Override
    public void onFooterViewRefresh() {
        loadDataFromServer(buildListener(RequestCode.SCROLL));

    }

    private void loadDataFromServer(GetQueryorderListenerY getOrderListener) {
        mPullListW.setVisibility(View.GONE);//隐藏未签收的listview
        mPullListW.setMode(PullToRefreshBase.Mode.DISABLED);
        mPullListY.setVisibility(View.VISIBLE);
        mFooterView.setVisibility(View.VISIBLE);
        status = "1";
        if (getOrderListener.getRequestCode() == RequestCode.PULL) {
            page = 1;
        }else if(getOrderListener.getRequestCode() == RequestCode.SCROLL) {
            page++;
        }
        IOrder.getInstance().getQueryOrderY(Integer.toString(storehouseid), Integer.toString(groupid), Integer.toString(buildid), startTime, endTime, status, page, getOrderListener);

    }

    /**
     * 初始化适配器
     */
    public void initAdapter() {
        mDataList = new ArrayList<OrderInfo>();
        mDataLists = new ArrayList<OrderInfoY>();

        mAdapter = new QueryOrderAdapter(mDataList,mContext);
        mAdapterY = new QueryOrderAdapterY(mDataLists,mContext);
        mPullListY.setAdapter(mAdapterY);
        storehouse = new ArrayList<CustomerSpinnerObject>();
        grouplist = new ArrayList<CustomerSpinnerObject>();
        buildinglist = new ArrayList<CustomerSpinnerObject>();

        partSpinnerAdapter = new SpinnerAdapter(this.getActivity(),storehouse);
        groupSpinnerAdapter = new SpinnerAdapter(this.getActivity(),grouplist);
        buildSpinnerAdapter = new SpinnerAdapter(this.getActivity(),buildinglist);

        mPartSpinnerPopWindow = new SpinerPopWindow(this.getActivity());
        mPartSpinnerPopWindow.setAdatper(partSpinnerAdapter);
        mGroupSpinnerPopWindow = new SpinerPopWindow(this.getActivity());
        mGroupSpinnerPopWindow.setAdatper(groupSpinnerAdapter);
        mBuildSpinnerPopWindow = new SpinerPopWindow(this.getActivity());
        mBuildSpinnerPopWindow.setAdatper(buildSpinnerAdapter);
    }

    /**
     * 初始化控件
     * @param mInflater
     */
    private void init(LayoutInflater mInflater) {
        mContext = getActivity().getApplicationContext();
        userInfo = IAuthor.getInstance().loadUserInfoSyn(PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));

        iv_actionbar_title = (TextView) mConvertView.findViewById(R.id.iv_actionbar_title);
        tv_actionbar_deliveryName = (TextView) mConvertView.findViewById(R.id.tv_actionbar_deliveryName);

        mPullListW = (PullToRefreshListView) mConvertView.findViewById(R.id.list2);//未签收
        mPullListY = (PullToRefreshListView) mConvertView.findViewById(R.id.list1);//已签收
        status_1 = (TextView) mConvertView.findViewById(R.id.status_1);//已签收
        status_2 = (TextView) mConvertView.findViewById(R.id.status_2);//未签收
        num = (TextView) mConvertView.findViewById(R.id.num);
        rl_choice_for_part = (RelativeLayout) mConvertView.findViewById(R.id.rl_choice_for_part);
        rl_choice_for_status = (RelativeLayout) mConvertView.findViewById(R.id.rl_choice_for_status);
        rl_choice_for_order = (RelativeLayout) mConvertView.findViewById(R.id.rl_choice_for_order);
        rl_choice_for_building = (RelativeLayout) mConvertView.findViewById(R.id.rl_choice_for_building);//单元楼

        tv_part_name = (TextView) mConvertView.findViewById(R.id.tv_part_name);//仓库选择下拉框
        tv_status = (TextView) mConvertView.findViewById(R.id.tv_time);
        tv_order = (TextView) mConvertView.findViewById(R.id.tv_order);//小区名称
        tv_building = (TextView) mConvertView.findViewById(R.id.tv_building);//单元楼名称

        mLoadingCover = (com.gz.logistics.widget.LoadingCover) mConvertView.findViewById(R.id.layout_loading_cover);
        mLoadingCover.setOnLoadingCoverRefreshListener(this);

        mFooterView = (FooterView) mInflater.inflate(R.layout.footer_view, null);
        mPullListY.getRefreshableView().addFooterView(mFooterView);

        mLoadingData = new Dialog(getActivity(),R.style.DialogTheme);
        mLoadingData.setContentView(R.layout.loading_data_dialog2);
        mLoadingData.setCanceledOnTouchOutside(false);
        mLoadingData.setCancelable(false);

        mCalendar = Calendar.getInstance();
        timeDialog = new DoubleDatePickerDialog(this.getActivity(), 0, this, mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DATE),true);
        /**
         * 未签收列表点击某一个订单
         */
        mPullListW.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(mContext, OrderDetailActivity.class);
                intent.putExtra("order_sn", mDataList.get(position - 1).getSn());
                intent.putExtra("status","2");
                startActivity(intent);
            }
        });
        /**
         * 已签收列表点击某一个订单
         */
        mPullListY.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(mContext, OrderDetailActivity.class);
                intent.putExtra("order_sn", mDataLists.get(position - 1).getSn());
                intent.putExtra("status","1");
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void ininDate() {
        iv_actionbar_title.setText("订单查询");
        tv_actionbar_deliveryName.setText("配送员：" + userInfo.getUser_name());
        mHandler.sendEmptyMessage(1);
        loadbuildingData();//默认加载未签收的订单列表
    }

    /**
     * 初始化各种监听器
     */
    private void initListener() {

        mPullListY.setOnRefreshListener(this);
        mPullListY.setOnScrollListener(mFooterView);

        mFooterView.setOnFooterViewRefreshListener(this);

        myGetStoreListener = new myGetStoreListener(this);
        myGetGroupListener = new myGetGroupListener(this);
        myGetBuildListener = new myGetBuildListener(this);
        myGetOrderListener = new MyGetQueryorderListener(this);

        rl_choice_for_part.setOnClickListener(this);
        rl_choice_for_status.setOnClickListener(this);
        rl_choice_for_order.setOnClickListener(this);
        rl_choice_for_building.setOnClickListener(this);
        status_1.setOnClickListener(this);
        status_2.setOnClickListener(this);

        mPartSpinnerListener = new MyPartSpinnerOnItemSelectListener(this);
        mGroupSpinnerListener = new MyGroupSpinnerOnItemSelectListener(this);
        mBuildSpinnerListener = new MyBuildSpinnerOnItemSelectListener(this);

        mPartSpinnerPopWindow.setItemListener(mPartSpinnerListener);
        mGroupSpinnerPopWindow.setItemListener(mGroupSpinnerListener);
        mBuildSpinnerPopWindow.setItemListener(mBuildSpinnerListener);

    }

    @Override
    public void onResume() {
        super.onResume();
        ininDate();
    }

    /**
     * 从服务器加载数据(未签收)
     */
    private void loadbuildingData() {
        mPullListY.setVisibility(View.GONE);
        mPullListW.setVisibility(View.VISIBLE);
        mPullListW.setMode(PullToRefreshBase.Mode.DISABLED);//禁止上拉下拉
        mFooterView.setVisibility(View.GONE);//隐藏底部
        status = "2";
        IOrder.getInstance().getQueryOrder(Integer.toString(storehouseid), Integer.toString(groupid), Integer.toString(buildid), startTime, endTime,status,myGetOrderListener);
    }

    /**
     * 首页未签收订单加载回调接口
     */
    private static class MyGetQueryorderListener extends GetQueryorderListener {
        private QureyOrderFragment mOuter;
        public MyGetQueryorderListener(QureyOrderFragment outer){
            this.mOuter = outer;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<OrderInfo> lists) {
            if(mOuter == null){
                return;
            }

            if (lists.size()==0){
                mOuter.num.setVisibility(View.GONE);
            }else{
                mOuter.num.setText("("+lists.size()+")");
            }
            mOuter.mDataList.clear();
            mOuter.mDataList.addAll(lists);
            mOuter.mPullListW.setAdapter(mOuter.mAdapter);
            mOuter.mAdapter.notifyDataSetChanged();
            mOuter.mLoadingCover.onRefreshSuccess();
            if (mOuter.mLoadingData.isShowing()) {
                mOuter.mLoadingData.dismiss();
            }
        }

        @Override
        public void onNoMoreData() {
            if(mOuter == null){
                return;
            }
            mOuter.mLoadingCover.setVisibility(View.VISIBLE);
            mOuter.num.setVisibility(View.GONE);
            mOuter.mLoadingCover.onRefreshFailed("暂无信息");
            if (mOuter.mLoadingData.isShowing()) {
                mOuter.mLoadingData.dismiss();
            }
        }

        @Override
        public void onNoNetWork() {
            if(mOuter == null){
                return;
            }
            mOuter.mLoadingCover.setVisibility(View.VISIBLE);
            mOuter.num.setVisibility(View.GONE);
            mOuter.mLoadingCover.onRefreshFailed("暂无信息");
            ToastUtils.showToast(mOuter.getActivity(), Define.MSG_NO_NETWORK);
            if (mOuter.mLoadingData.isShowing()) {
                mOuter.mLoadingData.dismiss();
            }
        }

        @Override
        public void onResponseFailed() {
            if(mOuter == null){
                return;
            }
            mOuter.mLoadingCover.onRefreshFailed();
            if (mOuter.mLoadingData.isShowing()) {
                mOuter.mLoadingData.dismiss();
            }
        }
    }

    /**
     * 获取仓库的监听器
     * 杜志娟 2016-03-14
     */
    public static class myGetStoreListener extends GetStoreHouseListener {
        private QureyOrderFragment mOuter;

        public myGetStoreListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<StoreHouse> storeHouses) {
            StoreHouse ignorePart = new StoreHouse();
            ignorePart.setStore_houseid(0);
            ignorePart.setStore_housename("全部");
            ignorePart.setIsSelected(true);
            storeHouses.add(0, ignorePart);

            mOuter.storehouse.clear();
            mOuter.storehouse.addAll(storeHouses);


        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }

    /**
     * 获取小区的监听器
     * 杜志娟 2016-03-14
     */
    public static class myGetGroupListener extends GetCommunityGroupListener {
        private QureyOrderFragment mOuter;

        public myGetGroupListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<CommunityGroup> groups) {
            CommunityGroup groupinfp = new CommunityGroup();
            groupinfp.setGroup_id(0);
            groupinfp.setGroup_name("全部");
            groupinfp.setIsSelected(true);
            groups.add(0, groupinfp);

            mOuter.grouplist.clear();
            mOuter.grouplist.addAll(groups);


        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }

    /**
     * 获取单元楼的监听器
     * 杜志娟 2016-03-14
     */
    public static class myGetBuildListener extends GetCommunityBuildingListener {
        private QureyOrderFragment mOuter;

        public myGetBuildListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<Building> buildings) {
            Building buildinfo = new Building();
            buildinfo.setBuilding_id(0);
            buildinfo.setBuilding_name("全部");
            buildinfo.setIsSelected(true);
            buildings.add(0, buildinfo);

            mOuter.buildinglist.clear();
            mOuter.buildinglist.addAll(buildings);



        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }


    public static class MyHandler extends android.os.Handler{
        private QureyOrderFragment mOuter;

        public MyHandler(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }
        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    IOrder.getInstance().getStoreHouse(Integer.toString(mOuter.userInfo.getUser_id()),mOuter.myGetStoreListener);
                    break;
            }
        }
    }

    /**
     *仓库下拉列表的 item 点击事件监听器
     * 杜志娟 2016-03-14
     */
    public static class MyPartSpinnerOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private QureyOrderFragment mOuter;

        public MyPartSpinnerOnItemSelectListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.mLoadingData.show();
            mOuter.storehouseid = ((StoreHouse)mOuter.storehouse.get(position)).getStore_houseid();//查询小区所需仓库的id
            mOuter.groupid = 0;
            mOuter.buildid = 0;
            for(int j = 0 ; j < mOuter.storehouse.size() ; j++){ // 把选中的仓库置为选中状态，
                if(j == position){
                    (mOuter.storehouse.get(j)).setIsSelected(true);
                    //调用查询其下的小区的接口
                    IOrder.getInstance().getCommunityGroup(Integer.toString(mOuter.storehouseid), mOuter.myGetGroupListener);
                }else{
                    (mOuter.storehouse.get(j)).setIsSelected(false);
                }
            }
            if (mOuter.status.equals("1")){
                mOuter.loadDataFromServer(mOuter.buildListener(RequestCode.PULL));
            }else{
                mOuter.loadbuildingData();
            }
            mOuter.grouplist.clear();
            mOuter.groupSpinnerAdapter.notifyDataSetChanged();
            mOuter.buildinglist.clear();
            mOuter.buildSpinnerAdapter.notifyDataSetChanged();
        }
    }
    /**
     *小区下拉列表的 item 点击事件监听器
     * 杜志娟 2016-03-14
     */
    public static class MyGroupSpinnerOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private QureyOrderFragment mOuter;

        public MyGroupSpinnerOnItemSelectListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.mLoadingData.show();
            mOuter.groupid = ((CommunityGroup)mOuter.grouplist.get(position)).getGroup_id();//查询单元楼所需小区的id
            mOuter.buildid = 0;

            for(int j = 0 ; j < mOuter.grouplist.size() ; j++){ // 把选中的小区置为选中状态，
                if(j == position){
                    (mOuter.grouplist.get(j)).setIsSelected(true);
                    //调用查询其下的单元楼的接口
                    IOrder.getInstance().getBuildings(Integer.toString(mOuter.groupid), mOuter.myGetBuildListener);
                }else{
                    (mOuter.grouplist.get(j)).setIsSelected(false);
                }
            }

            if (mOuter.status.equals("1")){
                mOuter.loadDataFromServer(mOuter.buildListener(RequestCode.PULL));
            }else{
                mOuter.loadbuildingData();
            }
            mOuter.buildinglist.clear();
            mOuter.buildSpinnerAdapter.notifyDataSetChanged();
        }
    }
    /**
     *单元楼下拉列表的 item 点击事件监听器
     * 杜志娟 2016-03-14
     */
    public static class MyBuildSpinnerOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private QureyOrderFragment mOuter;

        public MyBuildSpinnerOnItemSelectListener(QureyOrderFragment mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<QureyOrderFragment>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.mLoadingData.show();
            mOuter.buildid = ((Building)mOuter.buildinglist.get(position)).getBuilding_id();//查询单元楼所需小区的id

            for(int j = 0 ; j < mOuter.buildinglist.size() ; j++){ // 把选中的小区置为选中状态，
                if(j == position){
                    (mOuter.buildinglist.get(j)).setIsSelected(true);

                }else{
                    (mOuter.buildinglist.get(j)).setIsSelected(false);
                }
            }

            if (mOuter.status.equals("1")){
                mOuter.loadDataFromServer(mOuter.buildListener(RequestCode.PULL));
            }else{
                mOuter.loadbuildingData();
            }

        }
    }


    /**
     *  弹出时间选择 Dialog，并选择对应开始和介素时间后，点击确定按钮，将会触发以下方法
     *
     */
    @Override
    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth, DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth) {
        startTime = String.format("%d-%d-%d",startYear,startMonthOfYear+1,startDayOfMonth)+" 00:00:00";
        endTime = String.format("%d-%d-%d",endYear,endMonthOfYear+1,endDayOfMonth)+" 23:59:59";
        CrashHandler.i(TAG, "开始时间=" + startTime + "结束时间=" + endTime);
        mLoadingData.show();
        if (status.equals("1")){
            loadDataFromServer(buildListener(RequestCode.PULL));
        }else{
            loadbuildingData();
        }
    }
    /**
     * 首页已签收订单加载回调接口
     */
    private GetQueryorderListenerY buildListener (RequestCode requestCode) {
        return new GetQueryorderListenerY(requestCode) {
            @Override
            public void onResponseFailed() {
                // TODO Auto-generated method stub
                mLoadingCover.onRefreshFailed();
                mFooterView.onRefreshFailed();
                if (mPullListY.isRefreshing()) {
                    mPullListY.getLoadingLayoutProxy().refreshFailed();
                }
                if (mLoadingData.isShowing()) {
                    mLoadingData.dismiss();
                }
            }

            @Override
            public void onOffline() {
                // TODO Auto-generated method stub
                mLoadingCover.onRefreshFailed(Define.MSG_OFFLINE_ERROR);
                if (mPullListY.isRefreshing()) {
                    mPullListY.getLoadingLayoutProxy().refreshFailed();
                }
                if (mLoadingData.isShowing()) {
                    mLoadingData.dismiss();
                }
            }

            @Override
            public void onNoNetWork() {
                // TODO Auto-generated method stub
                if (mPullListY.isRefreshing()) {
                    mPullListY.getLoadingLayoutProxy().refreshFailed();
                }

                if (mLoadingData.isShowing()) {
                    mLoadingData.dismiss();
                }
                mFooterView.onRefreshFailed();
                mLoadingCover.onRefreshFailed(Define.MSG_NO_NETWORK);
            }

            @Override
            public void onNoMoreData() {
                // TODO Auto-generated method stub
                if (mPullListY.isRefreshing()) {
                    mPullListY.getLoadingLayoutProxy().refreshSuccess();
                }
                if (mLoadingData.isShowing()) {
                    mLoadingData.dismiss();
                }
                mFooterView.onRefreshComplete();
                if (page==1){
                    mLoadingCover.setVisibility(View.VISIBLE);
                    mLoadingCover.onRefreshFailed("暂无信息");}

            }

            @Override
            public void onResponseSuccess(List<OrderInfoY> orderInfoYs) {

                if (this.requestCode == RequestCode.PULL) {
                    mDataLists.clear();
                }
                mDataLists.addAll(orderInfoYs);
                mAdapterY.notifyDataSetChanged();
                if (orderInfoYs.size() < Define.LISTVIEW_ITEM_MIN_SIZE) {
                    mFooterView.onRefreshComplete();
                    mPullListY.setOnScrollListener(null);
                } else {
                    mAdapterY.notifyDataSetChanged();
                    mFooterView.onRefreshSuccess();//刷新成功并且后面还有数据可以进行加载
                    mPullListY.setOnScrollListener(mFooterView);
                }
                if (mPullListY.isRefreshing()) {
                    mPullListY.getLoadingLayoutProxy().refreshSuccess();
                }
                if (mLoadingData.isShowing()) {
                    mLoadingData.dismiss();
                }
                mLoadingCover.onRefreshSuccess();
            }
        };
    }
}
